/**
 * OSS Upload SDK 单元测试
 * 用于验证SDK核心功能的正确性
 */

const { OssUploadClient } = require('./dist/src/ossUpload');

// 模拟控制台输出，便于测试验证
const originalConsoleLog = console.log;
const logs = [];
console.log = (...args) => {
  logs.push(args.join(' '));
  originalConsoleLog.apply(console, args);
};

// 测试用的凭证函数
async function testGetCredentials() {
  return {
    accessKeyId: 'test-access-key-id',
    accessKeySecret: 'test-access-key-secret',
    securityToken: 'test-security-token',
    regionId: 'cn-hangzhou',
    bucketName: 'test-bucket',
    baseDir: 'test-dir'
  };
}

// 测试用的基础选项
const basicOptions = {
  accessKeyId: 'test-access-key-id',
  accessKeySecret: 'test-access-key-secret',
  bucket: 'test-bucket',
  region: 'oss-cn-hangzhou'
};

async function runTests() {
  let passedTests = 0;
  let totalTests = 0;

  function test(name, fn) {
    totalTests++;
    try {
      fn();
      console.log(`✅ ${name}`);
      passedTests++;
    } catch (error) {
      console.log(`❌ ${name}: ${error.message}`);
    }
  }

  function assertEqual(actual, expected, message) {
    if (actual !== expected) {
      throw new Error(`${message} - 期望: ${expected}, 实际: ${actual}`);
    }
  }

  function assertIncludes(text, substring, message) {
    if (!text.includes(substring)) {
      throw new Error(`${message} - 文本应包含: ${substring}`);
    }
  }

  console.log('开始运行OSS Upload SDK单元测试...\n');

  // 测试1: 类是否正确导出
  test('OssUploadClient类应该正确导出', () => {
    assertEqual(typeof OssUploadClient, 'function', 'OssUploadClient应该是一个函数');
  });

  // 测试2: 构造函数是否正常工作
  test('应该能够创建OssUploadClient实例', () => {
    const client = new OssUploadClient(basicOptions);
    assertEqual(typeof client, 'object', '应该返回一个对象实例');
  });

  // 测试3: 静态方法createWithCredentials
  test('createWithCredentials静态方法应该存在', () => {
    assertEqual(typeof OssUploadClient.createWithCredentials, 'function', 'createWithCredentials应该是一个函数');
  });

  // 测试4: 实例方法是否正确
  test('实例应该有uploadFile方法', () => {
    const client = new OssUploadClient(basicOptions);
    assertEqual(typeof client.uploadFile, 'function', 'uploadFile应该是一个函数');
  });

  test('实例应该有saveFile方法', () => {
    const client = new OssUploadClient(basicOptions);
    assertEqual(typeof client.saveFile, 'function', 'saveFile应该是一个函数');
  });

  test('实例应该有deleteFile方法', () => {
    const client = new OssUploadClient(basicOptions);
    assertEqual(typeof client.deleteFile, 'function', 'deleteFile应该是一个函数');
  });

  test('实例应该有updateCredentials方法', () => {
    const client = new OssUploadClient(basicOptions);
    assertEqual(typeof client.updateCredentials, 'function', 'updateCredentials应该是一个函数');
  });

  test('实例应该有getFileMimeType方法', () => {
    const client = new OssUploadClient(basicOptions);
    assertEqual(typeof client.getFileMimeType, 'function', 'getFileMimeType应该是一个函数');
  });

  // 测试5: getFileMimeType方法功能
  test('getFileMimeType应该正确返回MIME类型', () => {
    const client = new OssUploadClient(basicOptions);
    assertEqual(client.getFileMimeType('image.png'), 'image/png', 'PNG文件应该返回image/png');
    assertEqual(client.getFileMimeType('IMAGE.PNG'), 'image/png', '大写扩展名应该被正确处理');
    assertEqual(client.getFileMimeType('file.txt'), '', '未知扩展名应该返回空字符串');
  });

  // 测试6: createWithCredentials方法功能
  test('createWithCredentials应该能够创建配置好的实例', async () => {
    const client = await OssUploadClient.createWithCredentials(testGetCredentials);
    assertEqual(typeof client, 'object', '应该返回一个对象实例');
  });

  // 测试7: updateCredentials方法功能
  test('updateCredentials应该能够更新凭证', async () => {
    const client = new OssUploadClient(basicOptions);
    await client.updateCredentials(testGetCredentials);
    // 如果没有抛出异常，则测试通过
  });

  console.log(`\n测试完成: ${passedTests}/${totalTests} 个测试通过`);

  if (passedTests === totalTests) {
    console.log('🎉 所有测试通过！SDK功能验证成功。');
    return true;
  } else {
    console.log('❌ 部分测试失败，请检查代码。');
    return false;
  }
}

// 运行测试
runTests().then(success => {
  process.exit(success ? 0 : 1);
}).catch(error => {
  console.error('测试运行出错:', error);
  process.exit(1);
});